<template>
  <el-container id="ov">
    <el-header class="headerHome">
      <div class="heimaimg">
        <img src="../assets/heima.png"
             alt="">
        <span>电商后台管理系统</span>
      </div>
      <div>
        <el-button type="info"
                   @click="open">退出</el-button>
        <!-- 点击显示弹出框 删除token 跳转路由回到login -->
      </div>
    </el-header>
    <el-container>
      <el-aside class="aside"
                :width="isCollapse ? '64px':'200px'">
        <div @click="isState"
             class="isState">| | |</div>
        <el-menu class="el-menu-vertical-demo"
                 :default-active='defaultActive'
                 unique-opened
                 :collapse-transition='false'
                 :collapse="isCollapse"
                 router
                 background-color="rgb(51, 55, 68)"
                 text-color="#fff"
                 active-text-color="rgb(64, 158, 255)">
          <el-submenu :index="item1.path+''"
                      v-for="item1 in ListMenus"
                      :key="item1.id">

            <template slot="title">
              <i :class="icon[item1.id]"></i>
              <span>{{item1.authName}}</span>
            </template>

            <el-menu-item-group v-for="item2 in item1.children"
                                @click.native='itemHandler(item2.path)'
                                :key="item2.id">
              <el-menu-item :index="item2.path+''"> <i class="el-icon-menu"></i>{{item2.authName}}</el-menu-item>
            </el-menu-item-group>
          </el-submenu>
        </el-menu>

      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { menusApi } from '../api'
export default {
  props: {},
  data () {
    return {
      defaultActive: localStorage.getItem('defaultActive') || 'users',
      isCollapse: false,
      ListMenus: '',
      icon: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      }
    }
  },
  methods: {
    open () {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        localStorage.removeItem('token')
        this.$router.push('/login')
        localStorage.removeItem('defaultActive')
        this.$message({
          type: 'success',
          message: '退出成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消退出'
        })
      })
    },
    isState () {
      this.isCollapse = !this.isCollapse
    },
    async Menus () {
      const res = await menusApi()
      this.ListMenus = res.data.data
    },
    itemHandler (path) {
      localStorage.setItem('defaultActive', path)
      this.defaultActive = path
    }
  },
  created () {
    this.Menus()
    setTimeout(() => {
      localStorage.removeItem('token')
      this.$router.push('/login')
    }, 7200000)
  }
}
</script>

<style scoped lang='less'>
.el-main {
  background-color: rgb(234, 237, 241);
  margin-bottom: 50px;
}
* {
  font-size: 12px;
}
i {
  margin-right: 10px;
}
.headerHome {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 60px;
  background-color: rgb(55, 61, 65);
  .heimaimg {
    display: flex;
    align-items: center;
    img {
      vertical-align: middle;
    }
    span {
      margin-left: 15px;
      font-size: 22px;
      color: #fff;
    }
  }
}
.el-container {
  height: 100%;
}
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
}

.aside {
  background-color: rgb(51, 55, 68);
  .el-menu {
    border: none;
  }
}
.aside ul li ul li {
  text-align: right;
}

/deep/.el-menu-item-group__title {
  display: none;
}
.isState {
  width: 100%;
  height: 26.1px;
  line-height: 26.1px;
  text-align: center;
  color: #fff;
  background-color: rgb(74, 80, 100);
  box-sizing: border-box;
  cursor: pointer;
}
</style>
